Simulating Artificial Animals with Neural Networks
Project Summary
This piece of software was created to showcase a basic neural network in C++. The application allows the user to view how a neural network is trained over generations to look for food to survive.
Personal Writeup
One day, I was scrolling youtube like any other normal saturday. I stumbled upon an armada of youtube videos covering the simulation of artificial life and evolutionary behaviour. These videos ranged from virtual evolving creatures to the evolution of digital trees. Not all of them used neural networks, as I found some used other ways such as cellular automata or algorithms to achieve a form of digital life. My first attempt at creating the simulation about 2 weeks, which ended up not working in the long run. My code quality was inflexible and hard to manage and the creatures were not training for some reason. Using the dot product of the angle of the animal direction and angle needed to face the food caused the creatures to actually work! The dot product turns 2 Vector2s into a single value. The value in our case compares how similarly the 2 angles are. By using that as an input instead of a raw radian, the animals soon learned. By the end, I had a full working simulation. I hope to talking some more complicated neural network layouts or cellular automata that actually simulates plantgrowth or life.
Screenshot of the program
Documentation
Simulation Controls
- KEY_F: Reset Simulation
- KEY_R: Toggle between 1D and 2D velocity (may affect animal performance)
- KEY_SPACE: Toggle between 1x time and 60x time
File Controls
- KEY_ENTER: Submit file action
- KEYNUM_1: Toggle Creating file flag (only export)
- KEYNUM_2: Toggle Import or export flag
- KEYNUM_3: Cycle files
- KEYNUM_4: Toggle whether to use historic generation or current when writing to file (only export)
Sample of Import GUI
Sample of Export GUI
Statistics
Sample of information GUI
- Elapsed: Simulation runtime
- Time: Generation runtime (limit 60 sec before reset)
- Generation: Current iteration of simulation
- Best Score: Best historical fitness score. When saving by historical, it will grab this dataset
- Generation Best: Current iteration's best fitness score
Animal Structure
Screenshot of 2 animals searching for food
The first value is health while the 2nd one is the fitness value. The fitness value determines the overall success the animal had at getting food. Once health reaches zero, they die.
This graph shows the brainwave of the best performing animal
The output of the animal's brain determine how it moves around our area.
Specifications
- C++
- Raylib
- Visual Studio
- Imgui
- 1000x1000 Resolution
- Windows 10 and 11 Tested